package servlets;

import aggregate.aggregator.remote.ClientRemoteImpl;
import aggregate.aggregator.remote.Comment;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.CommentEntityFacadeLocal;
import dao.ContentFavoritesEntityFacadeLocal;
import dao.ContentStatisticEntityFacadeLocal;
import dao.RateEntityFacadeLocal;
import entities.CommentEntity;
import entities.ContentEntity;
import entities.ContentFavoritesEntity;
import entities.ContentStatisticEntity;
import entities.RateEntity;
import entities.UserEntity;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;

/**
 * 
 * @author andrei costache
 *
 */
public class ContentFullViewController extends ContentController{
	
        @EJB CommentEntityFacadeLocal commentDAO;
        @EJB ContentStatisticEntityFacadeLocal statisticsDAO;
        @EJB RateEntityFacadeLocal rateDAO;
        @EJB ContentFavoritesEntityFacadeLocal favoritesDAO;
	
	@Override
	public void doRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
            RequestDispatcher view = null;
            String cid = request.getParameter("cid");
            if (cid != null){
                ContentEntity content = contentDAO.find(new Long(cid));
                //loar or create a statitic object.
                ContentStatisticEntity statistic = statisticsDAO.findByContentId(new Long(cid));

                if (statistic == null){
                    statistic = new ContentStatisticEntity();
                    statistic.setCid(new Long(cid));
                    statistic.setComment_number(new Long(0));
                    statistic.setRating(new Double(0));
                    statistic.setViews(new Long(1));
                    statisticsDAO.create(statistic);
                } else {
                    statistic.setViews(statistic.getViews()+1);
                }
                //Check if a new comment has to be stored.
                String submitted = request.getParameter("submitted");
                if (submitted != null){
                    HttpSession session = request.getSession();
                    CommentEntity comment = new CommentEntity();
                    UserEntity user = (UserEntity)session.getAttribute("user");
                    if (user != null){
                        comment.setAuthor(user.getFirstname()+" "+user.getLastname());
                    } else {
                        comment.setAuthor("Annonymous");
                    }
                    comment.setComment(request.getParameter("comment"));
                    comment.setCon_id(content.getCid());
                    comment.setChanged(System.currentTimeMillis()/1000);
                    comment.setCreatd(System.currentTimeMillis()/1000);
                    comment.setParent_id(new Long(0));
                    comment.setSubject(request.getParameter("subject"));
                    //TODO improve code below
                    //user can be null here, uid might cause a big issue!
                    if (user != null) comment.setUid(user.getUid());
                    else comment.setUid(new Long(-1));
                    //To Do: announce the server here like in RateContentController.
                    //ClientRemoteImpl client = ClientRemoteImpl.getInstance();
                    //Comment comm = new Comment(comment.getComment(),comment.getSubject(), comment.getAuthor());
                    //or
                    //Comment comm = new Comment(Integer.parseInt(cid), comment.getComment(), comment.getSubject(), comment.getAuthor(),new Timestamp(comment.getCreatd()));
                    //TODO maybe set comment id
                    //comm.setId(???);
                    //client.commentAdded(Integer.parseInt(cid), comm);

                    commentDAO.create(comment);
                    statistic.setComment_number(statistic.getComment_number()+1);
                    view = request.getRequestDispatcher("/jsp/CommentsViewAjax.jsp");
                } else {
                    view = request.getRequestDispatcher("/jsp/ContentFullView.jsp");
                }
                //save the statistics.
                statisticsDAO.edit(statistic);
                //Inject the content and the comments.
                request.setAttribute("content", content);
                request.setAttribute("comments", commentDAO.findAllByContent(content.getCid()));
                //Inject also the statistic and the favorite option;
                request.setAttribute("statistic", statistic);
                HttpSession session = request.getSession();
                UserEntity user = (UserEntity)session.getAttribute("user");
                if (user == null) {
                    msg.addStatusMessage("Please login in order to use the system.");
                    request.setAttribute("messages",msg);
                    view = request.getRequestDispatcher("/jsp/UserLogin.jsp");
                }
                else {
                    RateEntity rating = rateDAO.findByUidAndCid(user.getUid(), new Long(cid));
                    request.setAttribute("rating",rating);
                    ContentFavoritesEntity favorite = favoritesDAO.findByUidAndCid(user.getUid(), new Long(cid));
                    request.setAttribute("favorite",favorite);
                    //view.forward(request, response);
                }
                view.forward(request, response);
            }
            
	}
	

}
